package io.plus.engine.client;

import io.plus.engine.pulse.AsynchronousSocketChannelContext;
import io.plus.engine.pulse.Configuration;
import io.plus.engine.pulse.Node;
import lombok.Data;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.atomic.AtomicInteger;

@Data
public class ClientAsynchronousSocketChannelContext extends AsynchronousSocketChannelContext {

    private String bindIp;

    private Integer bindPort;

    private ReConnectTask reConnectTask;

    /**
     * 连续重连次数，连接成功后，此值会被重置0
     */
    private AtomicInteger reConnectCount = new AtomicInteger();

    public ClientAsynchronousSocketChannelContext(Configuration configuration, AsynchronousSocketChannel asynchronousSocketChannel) {
        super(configuration, asynchronousSocketChannel);
    }


    /**
     * @param asynchronousSocketChannel
     * @see AsynchronousSocketChannelContext#createClientNode(java.nio.channels.AsynchronousSocketChannel)
     */
    @Override
    public Node createClientNode(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
        InetSocketAddress inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getLocalAddress();
        Node clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
        return clientNode;
    }


    @Override
    public boolean isServer() {
        return false;
    }


}
